package com.briup.srms.exam.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.briup.srms.exam.bean.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.briup.srms.exam.bean.QuestionBank;
import com.briup.srms.exam.bean.QuestionOptions;
import com.briup.srms.exam.bean.extend.QuestionExtend;
import com.briup.srms.exam.dao.QuestionBankMapper;
import com.briup.srms.exam.dao.QuestionMapper;
import com.briup.srms.exam.dao.QuestionOptionsMapper;
import com.briup.srms.exam.dao.extend.QuestionExtendMapper;
import com.briup.srms.exam.service.IQuestionService;
import com.briup.srms.exam.vm.QuestionWithOptions;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;

@Service
public class QuestionServiceImpl implements IQuestionService {
	@Autowired
	private QuestionMapper questionMapper;
	@Autowired
	private QuestionExtendMapper questionExtendMapper;
	@Autowired
	private QuestionOptionsMapper optionsMapper;
	@Autowired
	private QuestionBankMapper qbMapper;
	@Override
	public PageVM<QuestionExtend> pageQuery(int page, int pageSize,Long questionBankId,String type,String level,String label) {
		List<QuestionExtend> list = questionExtendMapper.query(page, pageSize, questionBankId, type, level, label);
		long total = questionExtendMapper.count(questionBankId, type, level, label);
		return new PageVM<>(page, pageSize, total, list);
	}

	@Override
	@Transactional
	public void saveOrUpdate(QuestionWithOptions questionWithOptions) {
		// question 问题对象，options 所有问题的选项
		Question question = questionWithOptions.getQuestion();
		List<QuestionOptions> options = questionWithOptions.getOptions();
		// 判断保存还是修改
		if (question.getId() == null) {
			// 保存问题信息
			question.setCreateTime(new Date().getTime());
			questionMapper.insert(question);
			// 更新题库数量
			QuestionBank qb = qbMapper.selectByPrimaryKey(question.getQuestionBankId());
			if(qb != null) {
				switch (question.getType()) {
				case "单选":
					qb.setNumRadio(qb.getNumRadio()+1);
					break;
				case "多选":
					qb.setNumCheck(qb.getNumCheck()+1);
					break;
				case "填空":
					qb.setNumCompletion(qb.getNumCompletion()+1);
					break;
				case "判断":
					qb.setNumJudge(qb.getNumJudge()+1);
					break;
				case "问答":
					qb.setNumAnswer(qb.getNumAnswer()+1);
					break;
				default:
					break;
				}
				qb.setNumTotal(qb.getNumTotal()+1);
				qbMapper.updateByPrimaryKey(qb);
			}

			// 保存选项
			long questionId = question.getId();
			switch (question.getType()) {
			case "单选":
				// 保存选项
				long answerOptionId = 0;
				for (QuestionOptions option : options) {
					option.setQuestionId(questionId);
					optionsMapper.insert(option);
					if(option.getIsCorrect() == 1) {
						answerOptionId = option.getId();
					}
				}
				// 设置正确答案
				if(answerOptionId == 0) {
					throw new CustomerException("没有设置正确答案");
				}
				question.setAnswer(answerOptionId+"");
				questionMapper.updateByPrimaryKey(question);
				break;
			case "多选":
				// 保存选项
				List<Long> answerOptionIds = new ArrayList<Long>();
				for (QuestionOptions option : options) {
					option.setQuestionId(questionId);
					optionsMapper.insert(option);
					if(option.getIsCorrect() == 1) {
						answerOptionIds.add(option.getId());
					}
				}
				if(answerOptionIds.size() == 0) {
					throw new CustomerException("没有设置正确答案");
				}
				question.setAnswer(StringUtils.collectionToCommaDelimitedString(answerOptionIds));
				questionMapper.updateByPrimaryKey(question);
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void deleteById(long id) {
		// TODO Auto-generated method stub
		Question question = questionMapper.selectByPrimaryKey(id);
		if(question == null) {
			throw new CustomerException("要删除的数据不存在");
		}

		// 更新题库数量
		QuestionBank qb = qbMapper.selectByPrimaryKey(question.getQuestionBankId());
		if(qb != null) {
			switch (question.getType()) {
				case "单选":
					qb.setNumRadio(qb.getNumRadio()-1);
					break;
				case "多选":
					qb.setNumCheck(qb.getNumCheck()-1);
					break;
				case "填空":
					qb.setNumCompletion(qb.getNumCompletion()-1);
					break;
				case "判断":
					qb.setNumJudge(qb.getNumJudge()-1);
					break;
				case "问答":
					qb.setNumAnswer(qb.getNumAnswer()-1);
					break;
				default:
					break;
			}
			qb.setNumTotal(qb.getNumTotal()+1);
			qbMapper.updateByPrimaryKey(qb);
		}

		questionMapper.deleteByPrimaryKey(id);
	}
}
